package com.zerox.controller;

import com.zerox.entity.SysUser;
import com.zerox.service.SysUserService;
import com.zerox.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

//@RequestMapping(value = "/user")
@Controller
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String toLogin() {
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(HttpSession session, Model model, String username, String password) {
        SysUser sysUser = sysUserService.login(username, password);
        if (sysUser != null) {
            session.setAttribute("loginUser", sysUser);
            return "redirect:/user/tolist";
        }
        model.addAttribute("errorMsg", "用户或密码错误");
        return "login";
    }



    @ExceptionHandler(RuntimeException.class)
    public String handleLoginException(Model model,RuntimeException e) {
        model.addAttribute("errorMsg",e.getMessage());
        return "error";
    }






}


    //局部异常处理
    








    